﻿var box;
var boxSettints = {
    layerTitleTxt: "",
    noResultTipsTxt: "",
    sendUrl: "",
    sendDataPara: {},
    isShowCreateNewClassLink: false,
    flag: 0   //0院系1 班级 用于选择院系清除班级的值
}
var education = {};
// education.curSchoolType =@{@(TypeConfigs.GetSchoolRoot + 1)};
education.curSchoolType = 4071;
education.curLetter = "";
education.action = "create";
education.curEducation = -1;
education.createHtml = "";
education.initCreateBox = function (cobject) {
    var schoolType = $("select[name='school_type']");
    var isVisible = $("select[name='privacy']");
    var schoolName = $("#txtSchool");

    var schoolID = schoolName.next();
    var startDate = $(".setup_sel");
    var schoolCollege = $("#schoolCollege");
    var schoolCollegeID = schoolCollege.next();
    var schoolClass = $("#schoolClass");
    var schoolClassID = schoolClass.next();
    if (cobject) {

        schoolType.val(cobject.Type);
        isVisible.val(cobject.IsVisible);
        schoolName.val(cobject.SchoolName);
        schoolID.val(cobject.SchoolID);
        startDate.val(cobject.StartDate);
        schoolCollege.val(cobject.schoolCollege);
        schoolCollegeID.val(cobject.schoolCollegeID);
        schoolClass.val(cobject.SchoolClassName);
        schoolClassID.val(cobject.SchoolClassID);
    }
    //  App.Setup.BindInput(remark, "可以填写学院、班级、系列等信息，70个字以内", App.Setup.checkRemark);
    App.Setup.BindInput(startDate, "完整的入学年份，能够为你找到更多同届校友", null);
    $("#save").click(function () { education.submit(this); });
    App.Setup.BindSelect(isVisible);
    schoolName.focus(
              function () {
                  education.showSchoolBox($(this));
              }).keydown(function () { return false; });
    schoolType.change(function () {
        education.curSchoolType = $(this).val();
        App.EmptySchoolCollegeClass();
    });

    //院系操作
    $("#schoolCollege").focus(function () {
        boxSettints.layerTitleTxt = "选择院系";
        boxSettints.noResultTipsTxt = "还没找到符合条件的院系";
        boxSettints.sendUrl = "/Ajax/GetShoolsCollegeByLetLocType";
        boxSettints.sendDataPara = { schoolID: $("#school_id").val().ToInt() };
        boxSettints.isShowCreateNewClassLink = false;
        boxSettints.flag = 0;
        education.showCollegeClassBox($(this));
    }).keydown(function () { return false; });

    //班级操作
    $("#schoolClass").focus(function () {
        boxSettints.layerTitleTxt = "选择班级";
        boxSettints.noResultTipsTxt = "还没找到符合条件的班级";
        boxSettints.sendUrl = "/Ajax/GetShoolsClassByLetLocType";
        boxSettints.sendDataPara = { schoolID: $("#school_id").val().ToInt(), collegeID: $("#schoolCollege_id").val().ToInt() };
        boxSettints.isShowCreateNewClassLink = true;
        boxSettints.flag = 1;
        education.showCollegeClassBox($(this));
    }).keydown(function () { return false; });
}
education.init = function () {
    box = $("#schoolBox");
    var city = $("#city");
    var province = $("#province");
    var loadSchool = function () {
        var location = parseInt(city.val()) > 0 ? city.val() : (parseInt(province.val()) > 0 ? province.val() : -1);
        App.BindSchoolCtrl(location, education.curLetter, education.curSchoolType, function () {
        }, "/Ajax/GetShoolsByLetLocType/");
    };
    $(".btn_normal").add($(".close")).click(function () {
        box.hide();
        App.hideMash();
        return false;
    });

    $("#btnClose1").add($(".close")).click(function () {
        $("#schoolCollegeBox").hide();
        App.hideMash();
        return false;
    });
    $("#saveCreate").add($(".close")).click(function () {
        $("#CreateNewClassBox").hide();
        $("#schoolClass").val($("#newSchoolClass").val());
        $("#schoolClass_id").val("0");
        App.hideMash();
        return false;
    });
    box.find("a[letter='1']").click(function () {
        if ($("a[letter='2']")[0] == this) {
            education.curLetter = "";
            this.style.backgroundColor = "";
            $(this).attr("letter", 1);

        }
        else {
            if ($("a[letter='2']")[0]) {
                $("a[letter='2']")[0].style.backgroundColor = "";
                $("a[letter='2']").attr("letter", 1);
            }
            education.curLetter = $(this).attr("title");
            this.style.backgroundColor = "rgb(204, 190, 190)";
            $(this).attr("letter", 2);
        }
        loadSchool();
        return false;
    });
    App.BindCityCtrl(province, "province", 0, function () {
        App.BindCityCtrl(city, "City", province.val(), function () {
        }, "/Ajax/GetCity1/");
        province.hide();
        city.hide();
        province.show();
        city.show();
        loadSchool();
    }, "/Ajax/GetCity1/");
    province.change(function () {
        App.BindCityCtrl(city, "City", province.val(), function () { loadSchool(); }, "/Ajax/GetCity1/");
    });
    city.change(function () { loadSchool(); });
    var quiSearch = $("#quiSearch");
    App.autoComplete(quiSearch, function (id, name) {
        education.schoolTxtBox.val(name);
        education.schoolTxtBox.next().val(id);
        box.hide();
        App.hideMash();
    });
    this.createHtml = $("#divCreate .info_tab01").html();
    this.initCreateBox();

    $("#btnCreate a").click(function () {
        $("div[edit='1']").html("");
        $("#divCreate").show().append("<div class='info_tab01'>" + education.createHtml + "</div>");
        education.initCreateBox();
        $("#btnCreate").hide();
        education.action = "create";
        education.curEducation = -1;
    });

    $("#createNewClassLinkPara").click(function () {
        $("#CollegeClassBox").hide();
        App.hideMash();


        box = $("#CreateNewClassBox");
        var position = App.centerInScreen(box);
        box.css("left", position.left).css("top", position.top);
        App.showMash();
        this.schoolTxtBox = this;
        box.show();

        return false;
    });
};
education.showSchoolBox = function (ctrl) {
    box = $("#schoolBox");
    var position = App.centerInScreen(box);
    box.css("left", position.left).css("top", position.top);
    App.showMash();
    this.schoolTxtBox = ctrl;
    box.show();
    loadSyscschool();
};

education.showCollegeClassBox = function (ctrl) {

    box = $("#CollegeClassBox");
    var schoolID = boxSettints.sendDataPara.schoolID;
    if (schoolID < 1) {
        App.fullTip(CodeList["A00032"], 1000, null, 1);
        return false;
    }
    console.log(boxSettints.sendDataPara);
    $.ajax({
        dataType: "json",
        data: boxSettints.sendDataPara,
        url: boxSettints.sendUrl,
        cache: false,
        type: "post",
        success: function (o) {

            $("#layerTitlePara").html(boxSettints.layerTitleTxt);
            $("#noResultTipsPara").html(boxSettints.noResultTipsTxt);
            boxSettints.isShowCreateNewClassLink == false ? $("#createNewClassLinkPara").hide() : $("#createNewClassLinkPara").show();

            if (o.Code == "A00003")
                App.fullTip(CodeList[o.Code], 1000, null, 1);
            else if (o.Code == "A00017") {
                $("#noContent").show();
                $("#ContentUl").hide();
            } else {

                $("#noContent").hide();
                var schlist = $("#ContentUl").show();
                schlist.html("");
                $.each(o.Data, function () {
                    var item = $("<li  trueId='" + this.id + "' title='" + this.name + "'><a href='javascript:void(0);'>" + this.name + "</a></li>");
                    item.click(function () {
                        education.schoolTxtBox.val(item.attr("title"));
                        education.schoolTxtBox.next().val(item.attr("trueId"));
                        box.hide();
                        App.hideMash();
                        boxSettints.flag == 0 ? App.EmptyClass() : "";

                    });
                    schlist.append(item);
                });

            }
        },
        error: function () { App.fullTip(CodeList["A00003"], 1000, null, 1); }
    });
    box = $("#CollegeClassBox");
    var position = App.centerInScreen(box);
    box.css("left", position.left).css("top", position.top);
    App.showMash();
    this.schoolTxtBox = ctrl;
    box.show();
}


education.submit = function (alink) {
    var schoolType = $("select[name='school_type']");
    var isVisible = $("select[name='privacy']");
    var schoolName = $("#txtSchool");
    var startDate = $(".setup_sel");
    var schoolCollege = $("#schoolCollege");
    var educationList = $("#educationList");
    var schoolClass = $("#schoolClass");
    // App.Setup.checkRemark(remark);
    if ($.trim(schoolName.val()) != "" && $.trim(schoolClass.val()) != "") {
        alink.locked = true;
        $.ajax(
       {
           url: this.action == "create" ? "/Ajax/CreateEducation" : "/Ajax/EditEducation",
           datatype: "json",
           cache: false,
           data: { educationId: education.curEducation, isVisible: isVisible.val(), schoolType: schoolType.val(), schoolId: schoolName.next().val().ToInt(), schoolName: schoolName.val(), startdate: startDate.val(), schoolCollegeID: $.trim($("#schoolCollege_id").val().ToInt()), schoolCollege: $.trim(schoolCollege.val()), schoolClassID: $.trim($("#schoolClass_id").val().ToInt()), schoolClassName: $.trim(schoolClass.val()) },
           type: "post",
           success: function (o) {
               if (o.Code == "A00001") {
                   App.showLoginDial();
               }
               else if (o.Code == "A00003") {
                   App.FullMiniTip(CodeList["A00003"], alink, 2000, 1);
               }
               else {
                   App.FullMiniTip(CodeList[o.Code], alink, 1000, 3);
                   if (education.action == "create") {
                       if (o.Data != "") {
                           var first = educationList.find("div:first");
                           if (first[0])
                               $(o.Data).insertBefore(first);
                           else
                               educationList.html(o.Data);
                       }
                   }
                   else {
                       if (o.Data != "") {
                           curEduItem = $("#education_" + education.curEducation)
                           $(o.Data).insertBefore(curEduItem);
                           curEduItem.remove();
                       }
                   }
               }
               alink.locked = false;
           },
           error: function (request) {
               App.FullMiniTip(CodeList["A00003"], alink, 2000, 1);
               alink.locked = false;
           }
       });
    }
    else {
        education.showSchoolBox(schoolName);
    }
}
education.del = function (id) {
    App.confirm("是否真的删除？", function () {
        $.ajax(
       {
           url: "/Ajax/DelEducation",
           datatype: "json",
           cache: false,
           data: { educationId: id },
           type: "post",
           success: function (o) {
               if (o.Code == "A00001") {
                   App.showLoginDial();
               }
               else if (o.Code == "A00003") {
                   App.alert(CodeList["A00003"]);
               }
               else {
                   $("#education_" + id).remove();
               }
           },
           error: function (request) {
               App.alert(CodeList["A00003"]);
           }
       });
    });

}
education.edit = function (id) {
    $.ajax(
       {
           url: "/Ajax/getEducationDetail",
           datatype: "json",
           cache: false,
           data: { educationId: id },
           type: "post",
           success: function (o) {
               if (o.Code == "A00001") {
                   App.showLoginDial();
               }
               else if (o.Code == "A00003") {
                   App.alert(CodeList["A00003"]);
               }
               else {
                   $("div[edit='1']").html("");
                   var editElem = $("#educationEdit_" + id);
                   $("#divCreate").hide().find(".info_tab01").remove();
                   editElem.html("<div class='info_tab01'>" + education.createHtml + "</div>");
                   $("#btnCreate").show();
                   education.initCreateBox(o.Data);
                   education.action = "edit";
                   education.curEducation = id;
               }
           },
           error: function (request) {
               App.alert(CodeList["A00003"]);
           }
       });

}
education.schoolTxtBox = null;
$(function () {
    education.init();
    App.TimerFunArray.push(App.autoCompleteTimerEvent);
    App.curGetLink = "autoCompleteSchool";
    App.StartTimer();
});
function loadSyscschool() {
    var city = $("#city");
    var province = $("#province");

    var location = parseInt(city.val()) > 0 ? city.val() : (parseInt(province.val()) > 0 ? province.val() : -1);

    App.BindSchoolCtrl(location, education.curLetter, education.curSchoolType, function () {
    }, "/Ajax/GetShoolsByLetLocType/");
}